package com.vilinno.tlias.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {

    private static final String SECRET_KEY="dmlsaW5ubw==";
    private static final long EXPIRATION_TIME=12*60*60*1000;

    /**
     * 根据指定的密钥和有效期生成令牌
     * @param claims
     * @return
     */
    public static String generateToken(Map<String,Object> claims){
        return Jwts.builder().signWith(SignatureAlgorithm.HS256,SECRET_KEY)
                .addClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRATION_TIME))
                .compact();
    }

    /**
     * 解析传递来的token
     * @param token
     * @return
     */
    public static Claims parseToken(String token){
        return Jwts.parser().setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
    }

}
